$(document).ready(function () {

  //加载页面内容
  loadPage();

  //加载菜单
  loadMenu();

});

/**
 * 加载页面内容
 */
function loadPage(){
  //设置标题
  var zsmc = localStorage.getItem("zsmc");
  $('title').text(zsmc);
  $('#zsmc').text(zsmc);
}

function loadMenu(){
  queryNavigationMenu();
  querySubMenu("-2");
  $("#loginUser").text(localStorage.getItem("userName"));
  $(".navbar-minimalize").click(function () {
    querySubMenu(f_mcode);
    //显示隐藏log.png
    $(".navbar-brand").toggle();
    $("body").toggleClass("mini-navbar"),
        SmoothlyMenu()
  });
}

/**
 * 退出按钮操作
 */
function logout() {
  LayerWn.showConfirm("确定要退出吗？", function () {
    //删除本地token
    localStorage.removeItem("token");
    //跳转到登录页面
    location.href = '/sjzc';
    //删除用户名
    localStorage.removeItem("userName");
  });
}

//获取导航菜单
function queryNavigationMenu() {
  var zclsh = localStorage.getItem("zclsh");
  $.ajax({
    type: "GET",
    contentType: 'application/json',
    dataType: "json",
    url: "queryNavigationMenu?zclsh="+zclsh,
    success: function (jsonData) {
      if (jsonData.statusCode == 200) {
        var liHtml = '';
        var menuList = jsonData.repData.menulist;
        if (menuList.length > 0) {
          $.each(menuList, function (index, entity) {
            liHtml += '<li    class="firstMenu" onclick="show(\'' + entity.mcode + '\',this)">';
            liHtml += '<a onmousemove="menuMousemoveF(this)" onmouseout="menuMouseoutF(this)">';
            liHtml += '<i  class="firstIconA" style="background:url(' + entity.icon + ') no-repeat;padding-left:9px;padding-right:9px;"></i>';
            liHtml += '<i  class="firstIconB" style="background:url(' + entity.icon.replace(".png", "B.png") + ') no-repeat;padding-left:9px;padding-right:9px;display: none"></i>' +
                '<span>' + entity.mcaption + '</span></a>';
            liHtml += '</li>';
          })
        }
        $("#masterMenu").html(liHtml);
        $(".navwinning").find("li").eq(0).trigger("click");
      }
    }
  });
}

//全局参数 点击时 保存一级菜单id
var f_mcode = "-2";

//菜单事件
function show(mcode, obj) {
  $(".firstMenu").css("background", '').css("height", "");
  $(obj).css("background", 'url("img/xz.png") repeat');
  $(".navwinning-act").css("display", "none");
  var left = $(obj).offset().left - 15;
  $(obj).find(".navwinning-act").css("left", left + "px");
  $(obj).find(".navwinning-act").css("display", "block");

  //每次加载清空之前菜单
  // $('#side-menu').empty();
  f_mcode = mcode
  querySubMenu(mcode);

}


/**
 * 一级菜单鼠标移入菜单操作
 */
function menuMousemoveF(obj) {
  $(obj).children(".firstIconB").show();
  $(obj).children(".firstIconA").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMousemove(obj) {
  $(obj).children(".secondIconB").show();
  $(obj).children(".secondIconA").hide();
}


/**
 * 一级菜单鼠标移出菜单操作
 */
function menuMouseoutF(obj) {
  $(obj).children(".firstIconA").show();
  $(obj).children(".firstIconB").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMouseout(obj) {
  $(obj).children(".secondIconB").hide();
  $(obj).children(".secondIconA").show();
}

/**
 * 查询二三级菜单
 * @param mcode
 */
function querySubMenu(mcode) {
  var zclsh = localStorage.getItem("zclsh");
  $.ajax({
    type: "GET",
    contentType: 'application/json',
    dataType: "json",
    url: "querySubMenu?mcode="+mcode+"&zclsh="+zclsh,
    success: function (jsonData) {
      if (jsonData.statusCode == 200) {   //查询成功
        var secondlist = jsonData.repData.secondMenu;
        var menuHtml = '';
        if (secondlist.length > 0) {
          $.each(secondlist, function (index, entity) {

            var secondHtml = '';
            if ("1" == entity.subFlag) {
              secondHtml += '<li  style="margin:2px 0"  onclick="second_onclick(' + entity.mcode + ',this)">';
              secondHtml += '<a class="second-a" href="#" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
              secondHtml += '<img class="secondIconA" id="' + entity.mcode + 'A" src="img/icon/' + entity.mcode + 'A.png" style="margin-right:5px;margin-bottom: 4px;display: inline"></img>';
              secondHtml += '<img class="secondIconB" id="' + entity.mcode + 'B" src="img/icon/' + entity.mcode + 'B.png" style="display:none;margin-right:5px;margin-bottom: 4px"></img>';
              secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
              secondHtml += '</a>';
              if (null != entity.thirdlist) {
                var thirdHtml = '';
                if (entity.thirdlist.length > 0) {
                  secondHtml += '<ul class="nav-second-wn nav-second-level-wn">';
                  $.each(entity.thirdlist, function (index, thirdEntity) {
                    thirdHtml += '<li>';
                    thirdHtml += '<a id='+thirdEntity.mcode+' class="J_menuItem" style="padding-right: 0" href=' + thirdEntity.module + '?mcode=' + thirdEntity.mcode + '&cdType=third>' + thirdEntity.mcaption + '</a>';
                    thirdHtml += '</li>';
                  });
                  secondHtml += thirdHtml;
                  secondHtml += '</ul>';
                }
              }
              secondHtml += '</li>';
            } else {
              secondHtml += '<li>';
              secondHtml += '<a class="J_menuItem" style="padding-right: 0" href="layouts.html">';
              secondHtml += '<i class="fa fa-columns"></i>';
              secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
              secondHtml += '</a>';
              secondHtml += '</li>';
            }
            menuHtml += secondHtml;
          })
        }
        $('#side-menu').html(menuHtml);
        $("#side-menu").metisMenu();
        $(".J_menuItem").on("click", c);
        // var firstTarget = document.getElementById('101001001001');
        // firstTarget.click();
      } else {

      }
    }
  });

}

/**
 * 三级菜单选中之后其二级菜单底色也要成选中的颜色
 */
function second_onclick(param, obj) {
  $(obj).siblings().find("li a").css("background-color", "#FFF");
  $(".secondIconA").show();
  $(".secondIconB").hide();
  $("#" + param + "A").hide();
  $("#" + param + "B").show();
  if (!$('body').hasClass('mini-navbar'))
    $("body").removeClass("mini-navbar");
  else {
    $("body").toggleClass("mini-navbar");
    SmoothlyMenu();
  }
}
